package com.fandou.speech.tts.baidu;

import android.content.Context;

import com.baidu.tts.Synthesizer;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.fandou.speech.tts.ISpeaker;
import com.fandou.speech.tts.ISpeakerListener;

public class Speaker extends Synthesizer implements ISpeaker {
    private long speakerId;
    private ISpeakerListener speakerListener;

    @Override
    public void initialTts(Context context) {
        super.initialTts(context);
    }

    @Override
    public long startTts(String text) {
        return startTts(text, null);
    }

    @Override
    public long startTts(String text, ISpeakerListener listener) {
        speakerListener = listener;
        speak(text);
        speakerId = System.currentTimeMillis();
        return speakerId;
    }

    @Override
    public void resumeTts() {
        super.resume();
    }

    @Override
    public void pauseTts() {
        super.pause();
    }

    @Override
    public void stopTts() {
        super.stop();
    }

    @Override
    public void stopTts(long speakerId) {
        if (this.speakerId == speakerId) {
            stopTts();
        }
    }

    @Override
    public void releaseTts() {
        super.release();
    }

    @Override
    protected SpeechSynthesizerListener getSpeechSynthesizerListener() {
        return speechSynthesizerListener;
    }

    private SpeechSynthesizerListener speechSynthesizerListener = new SpeechSynthesizerListener(){

        @Override
        public void onSynthesizeStart(String s) {

        }

        @Override
        public void onSynthesizeDataArrived(String s, byte[] bytes, int i, int i1) {

        }

        @Override
        public void onSynthesizeFinish(String s) {

        }

        @Override
        public void onSpeechStart(String s) {

        }

        @Override
        public void onSpeechProgressChanged(String s, int i) {

        }

        @Override
        public void onSpeechFinish(String s) {
            if (speakerListener != null) {
                speakerListener.onSpeakOver();
            }
        }

        @Override
        public void onError(String s, SpeechError speechError) {

        }
    };
}
